﻿Ext.define('Admin.view.leaveapprove.LeaveApproveViewController', {
    extend: Ext.app.ViewController,
    alias: 'controller.leaveapprove',
    onClickLeaveApproveClaimButton: function(view, recIndex, cellIndex, item, e, record) {
        Ext.Ajax.request({
            url: 'leave/claim/' + record.get('taskId'),
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function() {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
                }
            }
        });
    },
    setCurrentView: function(view, title) {
        if (view) {
            var cfg = Ext.apply({
                    xtype: 'leaveapprove-window',
                    items: [
                        Ext.apply({
                            xtype: view
                        }, '')
                    ]
                }, {
                    title: title
                });
            Ext.create(cfg);
        }
    },
    	
    	
    onClickLeaveApproveCompleteWindowButton: function(view, recIndex, cellIndex, item, e, record) {
    	//选中点击的行
        var taskDefinitionKey = record.get('taskDefinitionKey');
        if (taskDefinitionKey == 'deptLeaderAudit') {
            //部门领导审批
            this.setCurrentView('leaveapprove_deptleaderAudit_form', '部门领导审批');
            var form = Ext.getCmp('leaveapprove_deptleaderAudit_form').getForm();
            this.setHiddenValue(form,record);
        } else if (taskDefinitionKey == 'hrAudit') {
        	//人事审批
        	this.setCurrentView('leaveapprove_hrAudit_form', '人事审批');
            var form = Ext.getCmp('leaveapprove_hrAudit_form').getForm();
            this.setHiddenValue(form,record);
        }
        else if (taskDefinitionKey == 'reportBack') {
        	 //申请人销假
        	this.setCurrentView('leaveapprove_reportBack_form', '销假');
            var form = Ext.getCmp('leaveapprove_reportBack_form').getForm();
            this.setHiddenValue(form,record);
        }
        else if (taskDefinitionKey == 'modifyApply') {
        	//申请人调整申请：可以编写到工具类中
        	this.setCurrentView('leaveapprove_modifyApply_form', '调整申请');
            var form = Ext.getCmp('leaveapprove_modifyApply_form').getForm();
            form.loadRecord(record);
        }
    },

    setHiddenValue:function(form,record){
        form.findField('taskId').setValue(record.get('taskId'));
        form.findField('id').setValue(record.get('id'));
        form.findField('taskDefinitionKey').setValue(record.get('taskDefinitionKey'));
    },
    
	complete: function(url, variables ,grid ,form,backReason){
		// 转换JSON为字符串
	    var keys = "", values = "", types = "";
        var realityStartTime = null;
        var realityEndTime = null;
		if (variables) {
			Ext.each(variables, function (item) {
				if (keys != "") {
					keys += ",";
					values += ",";
					types += ",";
				}
				keys += item.key;
				values += item.value;
				types += item.type;
            });
		}
        if (form.getForm().findField('realityStartTime')!=null&&form.getForm().findField('realityEndTime')!=null) {
            realityStartTime = Ext.Date.format(form.getForm().findField('realityStartTime').getValue(),'Y/m/d H:i:s');
            realityEndTime = Ext.Date.format(form.getForm().findField('realityEndTime').getValue(),'Y/m/d H:i:s');
            Ext.Ajax.request({
                url: url,
                method: 'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params : { 
                    id:form.getForm().findField('id').getValue(),
                    taskId:form.getForm().findField('taskId').getValue(),
                    taskDefinitionKey:form.getForm().findField('taskDefinitionKey').getValue(),
                    realityStartTime:realityStartTime,
                    realityEndTime:realityEndTime,
                    backReason:backReason,
                    keys: keys,
                    values: values,
                    types: types
                }, 
                success: function(response, options) {
                    var json = Ext.util.JSON.decode(response.responseText);
                    if (json.success) {
                        Ext.Msg.alert('操作成功', json.msg);
                        form.up('window').close();
                        if (grid!=null) {
                            grid.getStore().reload();
                        }
                        if (Ext.getCmp('leave-grid')!=null) {
                            Ext.getCmp('leave-grid').getStore().reload();
                        }
                        
                    } else {
                        Ext.Msg.alert('操作失败', json.msg);
                    }
                }
            });

        }else{
            Ext.Ajax.request({
                url: url,
                method: 'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params : { 
                    id:form.getForm().findField('id').getValue(),
                    taskId:form.getForm().findField('taskId').getValue(),
                    taskDefinitionKey:form.getForm().findField('taskDefinitionKey').getValue(),
                    backReason:backReason,
                    keys: keys,
                    values: values,
                    types: types
                }, 
                success: function(response, options) {
                    var json = Ext.util.JSON.decode(response.responseText);
                    if (json.success) {
                        Ext.Msg.alert('操作成功', json.msg);
                        form.up('window').close();
                        if (grid!=null) {
                            grid.getStore().reload();
                        }
                        if (Ext.getCmp('leave-grid')!=null) {
                            Ext.getCmp('leave-grid').getStore().reload();
                        }
                    } else {
                        Ext.Msg.alert('操作失败', json.msg);
                    }
                }
            });
        }
    		
	},
    	
    onClickDeptleaderAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('leaveapprove-grid');
        //提示必须选中一行记录
        //var selection = gridPanel.getSelectionModel().getSelection();
        //if(selection.length==0) {
	    //  	Ext.MessageBox.alert('提示','请选中一条审批记录，进行审批！');
	    //}else {
	    	var form = btn.up('form');
	    	var url = 'leave/complete';
	    	var variables = [{
				key: 'deptLeaderPass',
				value:form.getForm().findField('deptLeaderPass').getGroupValue(),//获取表单选择的value
				type: 'B'
			}];
            var backReason = form.getForm().findField('deptLeaderBackReason').getValue();
            this.complete(url,variables,gridPanel,form,backReason);
		//}
       
        
    },
    onClickHrAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('leaveapprove-grid');
            var form = btn.up('form');
            var url = 'leave/complete';
            var variables = [{
                key: 'hrPass',
                value: form.getForm().findField('hrPass').getGroupValue(),//获取表单选择的value
                type: 'B'
            }];
            var backReason = form.getForm().findField('hrBackReason').getValue();
            this.complete(url,variables,gridPanel,form,backReason);
        
    },

    onClickReportBackFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('leaveapprove-grid');
            var form = btn.up('form');
            var url = 'leave/complete';
            var variables = [{
                key: '',
                value: '',//获取表单选择的value
                type: ''
            }];
            var backReason = null;
            this.complete(url,variables,gridPanel,form,backReason);
    },

    onClickModifyApplyFormSubmitButton: function(btn) {
        var formPanel = btn.up('form');
        var obj = this;
        var date = new Date();
        var applyTime = date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.getDate()+'/'+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
        formPanel.getForm().submit({
            url:'leave/saveOrUpdate',
            method:'post',
            params: {
                applyTime:applyTime,
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                Ext.Msg.alert('提示', action.result.msg);
                var gridPanel = Ext.getCmp('leaveapprove-grid');
                
                var url = 'leave/complete';
                var variables = [{
                    key: ['reApply','applyUserId'],
                    value: [true,fullName],//获取表单选择的value
                    type: ['B','S']
                }];
                var backReason = null;
                obj.complete(url,variables,gridPanel,formPanel,backReason);
            },
            failure: function(form, action) {
                Ext.Msg.alert('提示', action.result.msg);
            }
        })

                 
    },

    onClickModifyApplyFormCancleButton: function(btn) {
        var gridPanel = Ext.getCmp('leaveapprove-grid');
        var form = btn.up('form');
        var url = 'leave/complete';
        var variables = [{
            key: 'reApply',
            value: false,//获取表单选择的value
            type: 'B'
        }];
        var backReason = null;
        this.complete(url,variables,gridPanel,form,backReason);
    }
});